
function timeInput (e)
{ 
	if (!e) return '00:00';
	if (e.indexOf ('am')>0||e.indexOf ('pm')>0)
	{
		return e;
	}
	var a=e.split (':');
	var ap=parseInt(a[0],10)>11 ? 'pm':'am';
	var hh=parseInt(a[0],10)>12 ? a[0]-12 : parseInt(a[0],10);
	if (hh==0) hh=12;
	return hh + ':' + a[1] + '' + ap; 
}
 
function isDate (e) { return !isNaN (new Date(e)); }
 
function dateUS (e)
{  
	if (!isNaN(e)) return e;
	if (e.indexOf ('-')>0)
	{
		var a=e.split ('-');
		return a[1]+'/'+a[2]+'/'+a[0];
	}
	return e;
}
 
function dateSQL (d)
{ 
	return d.getFullYear () + '-' + (d.getMonth() + 1) + '-' + d.getDate();
}
 
function dateUK (e)
{ 
	var d=new Date();
	if (e)
	{ 
		d=new Date (dateUS (e));
	}
	return dateSQL (d);
}
 
function dateInput (e)
{ 
    var value = e;
	var name=new Array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); 
	if (!value) value = '2008-09-12';
	value=dateUS (value);
	var d=new Date (value);
	if (isNaN (d)) return 'Invalid date "' + value + '"';
	var string = name[d.getMonth ()];
	string += ' ' + d.getDate();
	string += ', ' + d.getFullYear();
	return string;//new Date (e);
}

function toggle (name,icon)
{
	toggle_ex (name,icon,'inline')
}

window.keyupListener = new DHTMLEvent();
function onsearchkeyup(element)
{
	element.invoke = function (k)
	{
		HideTimeSelector ();
		this.title = 'Press enter to begin your search!';
		if (k==13) SearchEvents ();
	}
	element.onkeyup = function (e)
	{
		var x=e||window.event;
		window.keyupListener.invoke (x.keyCode);
	}
	window.keyupListener.subscribe (element);
}

function toggle_ex (name,icon,view)
{
	var ds=getElement (name);
	if (!ds) return false;
	var dn=ds.style.display;
	ds.style.display=dn=='none'?view:'none';
	if (icon) 
	{
	    var dc=getElement (icon);
	    if (dc) dc.src='/webservices/images/' + (dn=='none'?'minus':'plus') + '.gif';
	}
}

function toggle_tr (name)
{
	toggle_ex (name,null,document.all?'inline':'table-row')
}


function SectorPrint(element)
{
    var dz=getElement (element);
    if (dz)
    {   
		window.printdata = { title:window.report?window.reports[window.report].title:Toolbar.caption(),text:dz.innerHTML };
        var printer=window.open ('eb-print.html'); 
    }
}

function MouseRespond (o)
{
	if (o.tagName=='TD')
	{
		if (o.className!='datarow'&&o.className!='WeekName'&&o.className!='WeekDay'&&o.className!='WeekEnd') HideTimeSelector();
	}
}

function ucfirst (x)
{
	return x.substr(0,1).toUpperCase() + '' + x.substr(1);
}

function wsdl(request, callback, message, data) 
{   
    if (wsdl.caller.toString().indexOf ('serialize') < 0)
    {
        var name = wsdl.caller.toString();
        var re=/function\s+(\w+)/
        var mm=re.exec (name);
        if (mm) name = mm[1];
        if (!confirm ('wsdl is being called by legacy function\n    ' + name + '' + 
                      '\nThis is NOT an error but means part of the program ' + 
                      'is using an outdated script.')) return false;
    }
    if (window.isbusy)
	{
	//	confirm ("The connection is busy. Please try again later.");
		return false;
	} 
	if (document.all) window.http = XmlHttp.create(); //IE BUG FIX: Create new instance for each request
	window.http.onreadystatechange = callback; 
	window.processmessage = message ? message : 'Processing response...';  
	//window.isbusy = true; 
	Controller.LastRequestURL = request;
	if (window.disabler) window.disabler.invoke (window.isbusy);  
	try {  
		window.http.open(data?"POST":"GET", request, true); 
		window.http.send(data?data:''); 
	} catch (ex) { alert (ex.message); } 
}


function ajaxObject(url, callbackFunction, message) {   
    
    var that=this;   
    this.updating = false;
    this.abort = function() 
    {
        if (that.updating) 
        {
            that.updating=false;
            that.AJAX.abort();
            that.AJAX=null;
        }
    }
    
    window.processmessage = message ? message : 'Processing response...';  
    
    this.update = function(id, passData, postMethod) 
    { 
        if (that.updating) { return false; }
        that.id = id;                     
        that.AJAX=XmlHttp.create();                                             
        if (that.AJAX==null) 
        {                             
            return false;                               
        } 
        else 
        {
            that.AJAX.onreadystatechange = function() 
            {  
                if (that.AJAX.readyState==4) 
                {             
                    that.updating=false;                  
                    that.callback(that.AJAX.responseText, that.id);        
                    that.AJAX=null;                                         
                }                                            
            }                                                        
            that.updating = new Date();                              
            if (/post/i.test(postMethod)) 
            {
                var uri=urlCall+'?'+that.updating.getTime();
                that.AJAX.open("POST", uri, true);
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                that.AJAX.setRequestHeader("Content-Length", passData.length);
                that.AJAX.send(passData);
            } 
            else 
            {
                var uri=urlCall;
                that.AJAX.open("GET", uri, true);                             
                that.AJAX.send(null);                                         
            }              
            return true;                                             
        }                                                                           
    }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}




var HideTimeSelector = function ()
{
	var ds=getElement ('dtimeselect');
	if (ds) ds.style.display='none';
	var dr=getElement ('droomselect');
	if (dr) dr.style.display='none';
	var dr=getElement ('ddateselect');
	if (dr) dr.style.display='none';
	if(window.eventdatepicker) window.eventdatepicker.onupdate=function () { }
}

function AttachResizer (element, offsetx, offsety)
{    
    var dc=getElement (element); 
    window.resizer.clear ();
    if (dc)
    { 
        var offx = offsetx, offy=offsety;
        dc.invoke = function ()
        {
            this.style.width = window_w() - offx;
            this.style.height = window_h() - docy (this) - 10;//window_h() - (offy || docy(this)+10);
        }
        window.resizer.subscribe (dc);
        dc.invoke();
    } 
}

function DHTMLEvent ()
{
	this.clear=function () { this.subscribers=[]; }
	this.subscribe=function (i) { this.subscribers.push (i); }
	this.invoke=function (i) { for (var x in this.subscribers) try {this.subscribers[x].invoke (i);} catch(ex)  { } }
	this.clear ();
}

function parse4ID(text)
{
    var re = /\{(\d+)\}/ ;
    var mm = re.exec (text);
    if (mm) return parseInt(mm[1]);
    return -1;
}

function SimpleXMLIterator (dom,c,v)
{
	this.eof         = false;
	this.dom         = dom;
	this.index       = -1;
	this.name        = '';
	this.value       = '';
	this.recordname  = c||'record';
	this.fieldname   = v||'name';
	this.data		 = [];
	
	this.movefirst=function ()
	{
		this.index = 0;
		this.eof = this.sync ();
	}
	
	this.movenext=function ()
	{
		this.index ++; 
		this.eof = this.sync ();  
	}
	
	this.sync=function ()
	{  
		this.data = []; 
		var name, records = this.dom.getElementsByTagName (this.recordname);
		if (records)
		{ 
			if (this.index < records.length)
			{
				var fields=records[this.index].childNodes; 
				if (fields)
				{ 
				    if (this.fieldname)
				    {  
					    for (var i=0;i<fields.length;i++)
					    {  
					        try
					        { name = fields[i].getAttribute (this.fieldname);
					          if (fields[i].childNodes[0])
					          {
					              if (fields[i].childNodes[0].nodeValue)
					                  this.data [name] = fields[i].childNodes[0].nodeValue;
					              else this.data [name] = '';
					          } else this.data [name] = '';
					        } catch (ex) {  }
					    } 
				    }
				    else if (fields.length > 0 && fields[0].nodeValue.length > 0) 
				    {   
				        this.data = fields[0].nodeValue; 
				    }
				    return false;
				}
			} 
		}
		return true; 
	}
	
	this.movefirst();
}

function getElement (id)
{
    return document.getElementById (id);
}


function SerializeGeneric (name, object)
{
    var xml='';  
    xml +='<' + name + '>\n';  
    xml +='\n  <r>'  
    for (var x in object) xml += '    <f n="' + x + '"><![CDATA[' + object[x] + ']]></f>\n';   
    xml +='\n  </r>'  
    xml +='</' + name + '>';  
    return xml;
}
function docx(e) {
	try 
	{
			for (var x=0,i=e;i.tagName!="BODY";i=i.offsetParent) 
		{
		    x += i.offsetLeft;
		}
	} 
	catch (ex) 
	{ 
	    x=0
	}
    return x;
} 

function docy(e) {  
	try 
	{  
        for (var y=0,i=e;i.tagName!="BODY";i=i.offsetParent) 
		{
		    y += i.offsetTop;
		}
	} 
	catch (ex) 
	{ 
	    y=0 
	}
    return y;
}  
function bite (x,y) { return x & y ? x - y : x + y; }

function window_w() { return document.all ? document.body.offsetWidth : window.innerWidth;}
function window_h() { return document.all ? document.body.offsetHeight : window.innerHeight;}


function timer () { }
timer.handler = null;
timer.span = 1000;
timer.start = function () { timer.index=window.setTimeout ('timer.next()', timer.span); }
timer.stop = function () { window.clearTimeout (timer.handler); timer.handler=null; }



function setCookie(name, value)
{
    var expiredays=30,exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(name)
{
    if (document.cookie.length>0)
    {
        var c_end, c_start=document.cookie.indexOf(name + "=");
        if (c_start!=-1)
        { 
            c_start=c_start + name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return null;
}


function CreateLink (text, click, title, css)
{
    return '<a title="' + (title || text) + '" style="' + css + '" href="javascript:void(0)" onclick="' + click + '">' + text + '</a>';
} 

function GetObjectByAttribute(object, attribute, value)
{
    for (var x in object)
    {
        if (object[x][attribute] == value) return object[x];
    } 
}







function secstotime (time) 
{
    var secs    = Math.floor(time);
    var hours   = Math.floor(secs / 3600);
    var minutes = Math.floor( (secs % 3600) / 60);
    var seconds = secs % 60;
    hours   = hours<10?"0"+hours:hours;
    minutes = minutes<10?"0"+minutes:minutes;
    seconds = seconds<10?"0"+seconds:seconds;
	return (hours>0?(hours+" hours and "):"")+minutes+' minutes'
    return (hours>0?(hours+":"):"")+minutes+":"+seconds+''
}   





